package com.ctshk.rpc.tour.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.SysApprovalType;
import com.ctshk.common.enums.SystemBusinessType;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.enums.TripStatusConstants;
import com.ctshk.common.exception.BusinessException;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.service.support.BaseService;
import com.ctshk.common.utils.Assist;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.common.utils.SnowflakeIdWorker;
import com.ctshk.rpc.system.dto.MainDataTouristTypeListDTO;
import com.ctshk.rpc.system.req.MainDataTouristTypeListReq;
import com.ctshk.rpc.system.service.IMainDataTouristTypeService;
import com.ctshk.rpc.tour.entity.*;
import com.ctshk.rpc.tour.mapper.SeriesTripDefaultCostMapper;
import com.ctshk.rpc.tour.mapper.SeriesTripMapper;
import com.ctshk.rpc.tour.mapper.SeriesTripTouristTypeMapper;
import com.ctshk.rpc.tour.req.*;
import com.ctshk.rpc.tour.service.ISeriesTripDefaultCostService;
import com.ctshk.rpc.workflow.dto.ApprovalStartDTO;
import com.ctshk.rpc.workflow.service.ISysApprovalService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 成本分摊默认 服务实现类
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-01-15
 */
@DubboService
public class SeriesTripDefaultCostServiceImpl extends ServiceImpl<SeriesTripDefaultCostMapper, SeriesTripDefaultCost> implements ISeriesTripDefaultCostService, BaseService {

    /**
     * 代理商渠道信息
     */
    @Autowired
    private SeriesTripChannelOfflineAgentCustomServiceImpl agentCustomService;

    /**
     * 自营渠道信息
     */
    @Autowired
    private SeriesTripChannelOfflineSelfCustomServiceImpl selfCustomService;

    /**
     * 官方平台
     */
    @Autowired
    private SeriesTripChannelOnlineSelfPlatformServiceImpl selfPlatformService;

    /**
     * 第三方平台
     */
    @Autowired
    private SeriesTripChannelOnlineThirdPlatformServiceImpl thirdPlatformService;

    /**
     * 行程主表信息
     */
    @Autowired
    private SeriesTripMapper seriesTripMapper;

    /**
     * 默认--团综合成本服务
     */
    @Autowired
    private SeriesTripDefaultCostGroupServiceImpl defaultCostGroupService;

    /**
     * 默认-- 代收费用服务
     */
    @Autowired
    private SeriesTripDefaultCostCollectionServiceImpl defaultCostCollectionService;

    /**
     * 默认--代收旅客类型服务
     */
    @Autowired
    private SeriesTripDefaultCostCollectionTouristTypeServiceImpl defaultCostCollectionTouristTypeService;

    /**
     * 默认--调整费用服务
     */
    @Autowired
    private SeriesTripDefaultFeeAdjustServiceImpl defaultFeeAdjustService;

    /**
     * 自定义--成本分摊主表
     */
    @Autowired
    private SeriesTripCustomCostServiceImpl customCostService;

    /**
     * 自定义--销售价格设置
     */
    @Autowired
    private SeriesTripCustomSalePriceServiceImpl customSalePriceService;


    /**
     * 自定义--团综合成本
     */
    @Autowired
    private SeriesTripCustomCostGroupServiceImpl customCostGroupService;

    /**
     * 自定义--团综合成本--自定义属性值
     */
    @Autowired
    private SeriesTripCustomCostGroupAgeServiceImpl customCostGroupAgeService;


    /**
     * 自定义--代收费用
     */
    @Autowired
    private SeriesTripCustomCostCollectionServiceImpl customCostCollectionService;

    /**
     * 自定义--代收费用--旅客类型
     */
    @Autowired
    private SeriesTripCustomCostCollectionTouristTypeServiceImpl customCostCollectionTouristTypeService;


    /**
     * 自定义模板-调整项目
     */
    @Autowired
    private SeriesTripCustomFeeAdjustServiceImpl customFeeAdjustService;

    /**
     * 自定义模板-调整项目--自定义值
     */
    @Autowired
    private SeriesTripCustomFeeAdjustTouristTypeServiceImpl customFeeAdjustTouristTypeService;


    /**
     * 自定义模板-日期段划分
     */
    @Autowired
    private SeriesTripCustomDateSubsectionServiceImpl customDateSubsectionService;

    /**
     * 自定义模板-系列团旅客类型
     */
    @Autowired
    private SeriesTripTouristTypeServiceImpl tripTouristTypeService;

    /**
     * 工作流服务
     */
    @DubboReference
    private ISysApprovalService sysApprovalService;
    
    @Autowired
    SeriesTripTouristTypeMapper seriesTripTouristTypeMapper;
    
    @DubboReference
    IMainDataTouristTypeService mainDataTouristTypeService;



    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result addSharing(TripCostSharingReq tripCostSharingReq, TokenUser tokenUser) {
        //主表设置模板类型
        SeriesTrip seriesTrip = seriesTripMapper.selectOne(new QueryWrapper<SeriesTrip>().lambda()
                .eq(SeriesTrip::getId, tripCostSharingReq.getTripId())
                .select(SeriesTrip::getId));
        if (null == seriesTrip) {
            throw new BusinessException(SystemError.SYS_411);
        }
        Long tripId = seriesTrip.getId();
        SeriesTrip trip = newUpdateEntity(SeriesTrip.class, null, tokenUser);
        trip.setId(tripId);
        trip.setModifiedId(tokenUser.getId());
        Integer costSharingType = tripCostSharingReq.getCostSharingType();
        trip.setCostSharingType(tripCostSharingReq.getCostSharingType());
        trip.setSelfSalePermissionType(tripCostSharingReq.getSelfSalePermissionType());
        trip.setAgentSalePermissionType(tripCostSharingReq.getAgentSalePermissionType());
        if (tripCostSharingReq.getCommitType() == 1) {
            trip.setStatus(1);
        }
        /*if (tripCostSharingReq.getCommitType() == 2) {

        }*/
        int update = seriesTripMapper.updateById(trip);
        if (update <= 0){
            throw new BusinessException(SystemError.SYS_408);
        }
        //自营渠道
        List<OwnChannelReq> ownChannelList = tripCostSharingReq.getOwnChannelList();
        if (CollectionUtils.isNotEmpty(ownChannelList)) {
            selfCustomService.remove(new QueryWrapper<SeriesTripChannelOfflineSelfCustom>().lambda().eq(SeriesTripChannelOfflineSelfCustom::getTripId, tripId));
            List<SeriesTripChannelOfflineSelfCustom> selfCustomList = EntityUtil.copyList(ownChannelList, SeriesTripChannelOfflineSelfCustom.class);
            fillCreateEntity(selfCustomList, tokenUser);
            selfCustomList.forEach(selfCustom->{
                selfCustom.setTripId(tripId);
                if(StringUtils.isEmpty(selfCustom.getAmount())){
                    selfCustom.setAmount(BigDecimal.ZERO);
                }
            });
            boolean batch = selfCustomService.saveBatch(selfCustomList);
            if (!batch){
                throw new BusinessException(SystemError.SYS_408);
            }
        }

        //代理商
        List<AgentChannelReq> agentChannelList = tripCostSharingReq.getAgentChannelList();
        if (CollectionUtils.isNotEmpty(agentChannelList)) {
            agentCustomService.remove(new QueryWrapper<SeriesTripChannelOfflineAgentCustom>().lambda().eq(SeriesTripChannelOfflineAgentCustom::getTripId, tripId));
            List<SeriesTripChannelOfflineAgentCustom> agentCustomList = EntityUtil.copyList(agentChannelList, SeriesTripChannelOfflineAgentCustom.class);
            fillCreateEntity(agentCustomList, tokenUser);
            agentCustomList.forEach(agentCustom->{
                agentCustom.setTripId(tripId);
                if(StringUtils.isEmpty(agentCustom.getAmount())){
                    agentCustom.setAmount(BigDecimal.ZERO);
                }
            });
            agentCustomService.saveBatch(agentCustomList);
        }
        //官方平台
        List<OfficialReq> officialList = tripCostSharingReq.getOfficialList();
        if (CollectionUtils.isNotEmpty(officialList)) {
            selfPlatformService.remove(new QueryWrapper<SeriesTripChannelOnlineSelfPlatform>().lambda().eq(SeriesTripChannelOnlineSelfPlatform::getTripId, tripId));
            List<SeriesTripChannelOnlineSelfPlatform> selfPlatformList = EntityUtil.copyList(officialList, SeriesTripChannelOnlineSelfPlatform.class);
            fillCreateEntity(selfPlatformList, tokenUser);
            selfPlatformList.forEach(selfPlatform->{
                selfPlatform.setTripId(tripId);
                if(StringUtils.isEmpty(selfPlatform.getAmount())){
                    selfPlatform.setAmount(BigDecimal.ZERO);
                }
            });
            selfPlatformService.saveBatch(selfPlatformList);
        }
        //第三方平台
        List<OtherPlatformsReq> otherPlatformsList = tripCostSharingReq.getOtherPlatformsList();
        if (CollectionUtils.isNotEmpty(otherPlatformsList)) {
            thirdPlatformService.remove(new QueryWrapper<SeriesTripChannelOnlineThirdPlatform>().lambda().eq(SeriesTripChannelOnlineThirdPlatform::getTripId, tripId));
            List<SeriesTripChannelOnlineThirdPlatform> thirdPlatformList = EntityUtil.copyList(tripCostSharingReq.getOtherPlatformsList(), SeriesTripChannelOnlineThirdPlatform.class);
            fillCreateEntity(thirdPlatformList, tokenUser);
            thirdPlatformList.forEach(thirdPlatform->{
                thirdPlatform.setTripId(tripId);
                if(StringUtils.isEmpty(thirdPlatform.getAmount())){
                    thirdPlatform.setAmount(BigDecimal.ZERO);
                }
            });
            thirdPlatformService.saveBatch(thirdPlatformList);
        }
        //新增默认成本分摊
        if (costSharingType == 1) {
            defaultCost(tripCostSharingReq, tripId,tokenUser.getId());
        }
        //新增自定义成本分摊
        if (costSharingType == 2) {
            customCost(tripCostSharingReq, tripId,tokenUser.getId());
        }
        return Result.success(trip.getId());
    }

    @Override
    public TripCostSharingReq sharing(Long id) {
        TripCostSharingReq tripCostSharingReq = new TripCostSharingReq();
        SeriesTrip seriesTrip = seriesTripMapper.selectById(id);
        if (null == seriesTrip) {
            throw new BusinessException(SystemError.SYS_411);
        }
        tripCostSharingReq.setTripId(seriesTrip.getId());
        Integer costSharingType = seriesTrip.getCostSharingType();
        tripCostSharingReq.setCostSharingType(costSharingType);
        tripCostSharingReq.setAgentSalePermissionType(seriesTrip.getAgentSalePermissionType());
        tripCostSharingReq.setSelfSalePermissionType(seriesTrip.getSelfSalePermissionType());
        //查询自营渠道
        List<SeriesTripChannelOfflineSelfCustom> selfCustomList = selfCustomService.list(new QueryWrapper<SeriesTripChannelOfflineSelfCustom>().lambda().eq(SeriesTripChannelOfflineSelfCustom::getTripId, id));
        List<OwnChannelReq> ownChannelList = EntityUtil.copyList(selfCustomList, OwnChannelReq.class);
        tripCostSharingReq.setOwnChannelList(ownChannelList);
        //代理商
        List<SeriesTripChannelOfflineAgentCustom> agentCustomList = agentCustomService.list(new QueryWrapper<SeriesTripChannelOfflineAgentCustom>().lambda().eq(SeriesTripChannelOfflineAgentCustom::getTripId, id));
        List<AgentChannelReq> agentChannelList = EntityUtil.copyList(agentCustomList, AgentChannelReq.class);
        tripCostSharingReq.setAgentChannelList(agentChannelList);
        //官方平台
        List<SeriesTripChannelOnlineSelfPlatform> selfPlatformList = selfPlatformService.list(new QueryWrapper<SeriesTripChannelOnlineSelfPlatform>().lambda().eq(SeriesTripChannelOnlineSelfPlatform::getTripId, id));
        List<OfficialReq> officialList = EntityUtil.copyList(selfPlatformList, OfficialReq.class);
        tripCostSharingReq.setOfficialList(officialList);
        //第三方平台
        List<SeriesTripChannelOnlineThirdPlatform> thirdPlatformList = thirdPlatformService.list(new QueryWrapper<SeriesTripChannelOnlineThirdPlatform>().lambda().eq(SeriesTripChannelOnlineThirdPlatform::getTripId, id));
        List<OtherPlatformsReq> otherPlatformsList = EntityUtil.copyList(thirdPlatformList, OtherPlatformsReq.class);
        tripCostSharingReq.setOtherPlatformsList(otherPlatformsList);
        //默认成本分摊
        if (costSharingType != null) {
            if (costSharingType == 2) {
                TripCustomTreeReq tripCustomTree = new TripCustomTreeReq();
                List<SeriesTripTouristType> seriesTripTouristTypeList = tripTouristTypeService.list(new QueryWrapper<SeriesTripTouristType>().lambda().eq(SeriesTripTouristType::getTripId, id).isNotNull(SeriesTripTouristType::getAgeBegin));
                List<TripCustomTreePassengerReq> passengerList = EntityUtil.copyList(seriesTripTouristTypeList, TripCustomTreePassengerReq.class);
                tripCustomTree.setPassengerList(passengerList);
                List<SeriesTripCustomDateSubsection> seriesTripCustomDateSubsectionList = customDateSubsectionService.list(new QueryWrapper<SeriesTripCustomDateSubsection>().lambda().eq(SeriesTripCustomDateSubsection::getTripId, id));
                List<TripCustomTreeDateReq> dateList = EntityUtil.copyList(seriesTripCustomDateSubsectionList, TripCustomTreeDateReq.class);
                tripCustomTree.setDateList(dateList);
                tripCostSharingReq.setTripCustomTree(tripCustomTree);
                //成本分摊主表
                List<SeriesTripCustomCost> customCostList = customCostService.list(new QueryWrapper<SeriesTripCustomCost>().lambda().eq(SeriesTripCustomCost::getTripId, id));
                List<TripCustomCostReq> tripCustomCostReqList = new ArrayList<>();
                TripCustomCostReq tripCustomCostReq = null;
                for (SeriesTripCustomCost seriesTripCustomCost : customCostList) {
                    tripCustomCostReq = new TripCustomCostReq();
                    tripCustomCostReq.setId(seriesTripCustomCost.getId());
                    tripCustomCostReq.setTripId(seriesTripCustomCost.getTripId());
                    tripCustomCostReq.setName(seriesTripCustomCost.getName());
                    tripCustomCostReq.setQuotationFileUrl(seriesTripCustomCost.getQuotationFileUrl());
                    tripCustomCostReq.setFileNotes(seriesTripCustomCost.getFileNotes());
                    List<SeriesTripCustomSalePrice> seriesTripCustomSalePriceList = customSalePriceService.list(new QueryWrapper<SeriesTripCustomSalePrice>().lambda().eq(SeriesTripCustomSalePrice::getCostId, seriesTripCustomCost.getId()));
                    List<TripCustomSalePriceReq> customSalePriceList = new ArrayList<>();
                    for (SeriesTripCustomSalePrice seriesTripCustomSalePrice : seriesTripCustomSalePriceList) {
                        TripCustomSalePriceReq tripCustomSalePriceReq = new TripCustomSalePriceReq();
                        BeanUtils.copyProperties(seriesTripCustomSalePrice, tripCustomSalePriceReq);
                        SeriesTripCustomDateSubsection seriesTripCustomDateSubsection = customDateSubsectionService.getById(seriesTripCustomSalePrice.getDateSubsectionId());
                        if (seriesTripCustomDateSubsection != null) {
                            tripCustomSalePriceReq.setDateType(seriesTripCustomDateSubsection.getDateType());
                            tripCustomSalePriceReq.setSubsectionId(seriesTripCustomDateSubsection.getSubsectionId());
                            tripCustomSalePriceReq.setSubsectionName(seriesTripCustomDateSubsection.getSubsectionName());
                            tripCustomSalePriceReq.setCheckWeekDays(seriesTripCustomDateSubsection.getCheckWeekDays());
                            tripCustomSalePriceReq.setDateBegin(seriesTripCustomDateSubsection.getDateBegin());
                            tripCustomSalePriceReq.setDateId(seriesTripCustomDateSubsection.getId());
                            tripCustomSalePriceReq.setDateEnd(seriesTripCustomDateSubsection.getDateEnd());
                        }
                        SeriesTripTouristType seriesTripTouristType = tripTouristTypeService.getById(seriesTripCustomSalePrice.getTripTouristTypeId());
                        if (seriesTripTouristType != null) {
                            tripCustomSalePriceReq.setPassengerTypeId(seriesTripTouristType.getId());
                            tripCustomSalePriceReq.setMainDataId(seriesTripTouristType.getMainDataId());
                            tripCustomSalePriceReq.setName(seriesTripTouristType.getName());
                            tripCustomSalePriceReq.setAgeBegin(seriesTripTouristType.getAgeBegin());
                            tripCustomSalePriceReq.setAgeEnd(seriesTripTouristType.getAgeEnd());
                        }
                        customSalePriceList.add(tripCustomSalePriceReq);
                    }
                    tripCustomCostReq.setCustomSalePriceList(customSalePriceList);

                    //团综合成本
                    List<SeriesTripCustomCostGroup> customCostGroupList = customCostGroupService.list(new QueryWrapper<SeriesTripCustomCostGroup>().lambda().eq(SeriesTripCustomCostGroup::getCostId, seriesTripCustomCost.getId()));
                    if (CollectionUtils.isNotEmpty(customCostGroupList)) {
                        List<SeriesTripCustomCostGroupReq> costGroupList = EntityUtil.copyList(customCostGroupList, SeriesTripCustomCostGroupReq.class);

                        for (SeriesTripCustomCostGroupReq seriesTripCustomCostGroupReq : costGroupList) {
                            List<CustomCostReq> customCostReqs = new ArrayList<>();
                            List<SeriesTripCustomCostGroupAge> customCostGroupAgeList = customCostGroupAgeService.list(new QueryWrapper<SeriesTripCustomCostGroupAge>().lambda().eq(SeriesTripCustomCostGroupAge::getGroupCostId, seriesTripCustomCostGroupReq.getId()));
                            for (SeriesTripCustomCostGroupAge seriesTripCustomCostGroupAge : customCostGroupAgeList) {
                                CustomCostReq customCostReq = new CustomCostReq();
                                customCostReq.setPrice(seriesTripCustomCostGroupAge.getPrice());
                                SeriesTripCustomDateSubsection seriesTripCustomDateSubsection = customDateSubsectionService.getById(seriesTripCustomCostGroupAge.getDateSubsectionId());
                                if (seriesTripCustomDateSubsection != null) {
                                    customCostReq.setDateId(seriesTripCustomDateSubsection.getId());
                                    customCostReq.setDateType(seriesTripCustomDateSubsection.getDateType());
                                    customCostReq.setSubsectionId(seriesTripCustomDateSubsection.getSubsectionId());
                                    customCostReq.setSubsectionName(seriesTripCustomDateSubsection.getSubsectionName());
                                    customCostReq.setCheckWeekDays(seriesTripCustomDateSubsection.getCheckWeekDays());
                                    customCostReq.setDateBegin(seriesTripCustomDateSubsection.getDateBegin());
                                    customCostReq.setDateEnd(seriesTripCustomDateSubsection.getDateEnd());
                                }
                                SeriesTripTouristType seriesTripTouristType = tripTouristTypeService.getById(seriesTripCustomCostGroupAge.getTripTouristTypeId());
                                if (seriesTripTouristType != null) {
                                    customCostReq.setPassengerTypeId(seriesTripTouristType.getId());
                                    customCostReq.setMainDataId(seriesTripTouristType.getMainDataId());
                                    customCostReq.setName(seriesTripTouristType.getName());
                                    customCostReq.setAgeBegin(seriesTripTouristType.getAgeBegin());
                                    customCostReq.setAgeEnd(seriesTripTouristType.getAgeEnd());

                                }
                                customCostReqs.add(customCostReq);
                            }
                            seriesTripCustomCostGroupReq.setCustomCostList(customCostReqs);
                        }

                        tripCustomCostReq.setCostGroupList(costGroupList);
                    }
                    //调整费用
                    List<SeriesTripCustomFeeAdjust> customFeeAdjustList = customFeeAdjustService.list(new QueryWrapper<SeriesTripCustomFeeAdjust>().lambda().eq(SeriesTripCustomFeeAdjust::getCostId, seriesTripCustomCost.getId()));
                    if (CollectionUtils.isNotEmpty(customFeeAdjustList)) {
                        List<SeriesTripCustomFeeAdjustReq> feeAdjustList = EntityUtil.copyList(customFeeAdjustList, SeriesTripCustomFeeAdjustReq.class);

                        for (SeriesTripCustomFeeAdjustReq seriesTripCustomFeeAdjustReq : feeAdjustList) {
                            List<CustomCostReq> customCostReqs = new ArrayList<>();
                            List<SeriesTripCustomFeeAdjustTouristType> customFeeAdjustTouristTypeList = customFeeAdjustTouristTypeService.list(new QueryWrapper<SeriesTripCustomFeeAdjustTouristType>().lambda().eq(SeriesTripCustomFeeAdjustTouristType::getAdjustId, seriesTripCustomFeeAdjustReq.getId()));
                            for (SeriesTripCustomFeeAdjustTouristType seriesTripCustomFeeAdjustTouristType : customFeeAdjustTouristTypeList) {
                                CustomCostReq customCostReq = new CustomCostReq();
                                customCostReq.setPrice(seriesTripCustomFeeAdjustTouristType.getPrice());
                                SeriesTripCustomDateSubsection seriesTripCustomDateSubsection = customDateSubsectionService.getById(seriesTripCustomFeeAdjustTouristType.getDateSubsectionId());
                                if (seriesTripCustomDateSubsection != null) {
                                    customCostReq.setDateId(seriesTripCustomDateSubsection.getId());
                                    customCostReq.setDateType(seriesTripCustomDateSubsection.getDateType());
                                    customCostReq.setSubsectionId(seriesTripCustomDateSubsection.getSubsectionId());
                                    customCostReq.setSubsectionName(seriesTripCustomDateSubsection.getSubsectionName());
                                    customCostReq.setCheckWeekDays(seriesTripCustomDateSubsection.getCheckWeekDays());
                                    customCostReq.setDateBegin(seriesTripCustomDateSubsection.getDateBegin());
                                    customCostReq.setDateEnd(seriesTripCustomDateSubsection.getDateEnd());
                                }

                                SeriesTripTouristType seriesTripTouristType = tripTouristTypeService.getById(seriesTripCustomFeeAdjustTouristType.getTripTouristTypeId());
                                if (seriesTripTouristType != null) {
                                    customCostReq.setPassengerTypeId(seriesTripTouristType.getId());
                                    customCostReq.setMainDataId(seriesTripTouristType.getMainDataId());
                                    customCostReq.setName(seriesTripTouristType.getName());
                                    customCostReq.setAgeBegin(seriesTripTouristType.getAgeBegin());
                                    customCostReq.setAgeEnd(seriesTripTouristType.getAgeEnd());
                                }
                                customCostReqs.add(customCostReq);
                            }
                            seriesTripCustomFeeAdjustReq.setCustomCostList(customCostReqs);
                        }

                        tripCustomCostReq.setFeeAdjustList(feeAdjustList);
                    }
                    //代收费用
                    List<SeriesTripCustomCostCollection> customCostCollectionList = customCostCollectionService.list(new QueryWrapper<SeriesTripCustomCostCollection>().lambda().eq(SeriesTripCustomCostCollection::getCostId, seriesTripCustomCost.getId()));
                    if (CollectionUtils.isNotEmpty(customCostCollectionList)) {
                        List<SeriesTripDefaultCostCollectionReq> costCollectionList = new ArrayList<>();
                        for (SeriesTripCustomCostCollection seriesTripCustomCostCollection : customCostCollectionList) {
                            SeriesTripDefaultCostCollectionReq seriesTripDefaultCostCollectionReq = new SeriesTripDefaultCostCollectionReq();
                            BeanUtils.copyProperties(seriesTripCustomCostCollection, seriesTripDefaultCostCollectionReq);
                            List<SeriesTripCustomCostCollectionTouristType> customCostCollectionTouristTypeList = customCostCollectionTouristTypeService.list(new QueryWrapper<SeriesTripCustomCostCollectionTouristType>().lambda().eq(SeriesTripCustomCostCollectionTouristType::getCollectionId, seriesTripCustomCostCollection.getId()));
                            //System.out.println("查出来的机场旅客---="+customCostCollectionTouristTypeList.size());
                            List<SeriesTripDefaultCostCollectionTypeReq> costCollectionTypeList = EntityUtil.copyList(customCostCollectionTouristTypeList, SeriesTripDefaultCostCollectionTypeReq.class);
                            //System.out.println("拷贝后的机场旅客---="+costCollectionTypeList.size());
                            seriesTripDefaultCostCollectionReq.setCostCollectionTypeList(costCollectionTypeList);
                            costCollectionList.add(seriesTripDefaultCostCollectionReq);
                        }
                        tripCustomCostReq.setCostCollectionList(costCollectionList);
                    }
                    tripCustomCostReqList.add(tripCustomCostReq);
                }
                tripCostSharingReq.setCustomCostReqList(tripCustomCostReqList);
            }
            //成本分摊主表
            if (costSharingType == 1) {

                List<SeriesTripDefaultCost> defaultCostList = baseMapper.selectList(new QueryWrapper<SeriesTripDefaultCost>().lambda().eq(SeriesTripDefaultCost::getTripId, id));
                List<TripDefaultCostReq> tripDefaultCostReqList = new ArrayList<>();
                TripDefaultCostReq tripDefaultCostReq = null;
                for (SeriesTripDefaultCost seriesTripDefaultCost : defaultCostList) {
                    tripDefaultCostReq = new TripDefaultCostReq();
                    tripDefaultCostReq.setId(seriesTripDefaultCost.getId());
                    tripDefaultCostReq.setTripId(seriesTripDefaultCost.getTripId());
                    SellingPriceReq sellingPriceReq = new SellingPriceReq();
                    BeanUtils.copyProperties(seriesTripDefaultCost, sellingPriceReq);
                    tripDefaultCostReq.setSellingPriceReq(sellingPriceReq);
                    //团综合成本
                    List<SeriesTripDefaultCostGroup> defaultCostGroupList = defaultCostGroupService.list(new QueryWrapper<SeriesTripDefaultCostGroup>().lambda().eq(SeriesTripDefaultCostGroup::getCostId, tripDefaultCostReq.getId()));
                    if (CollectionUtils.isNotEmpty(defaultCostGroupList)) {
                        List<SeriesTripDefaultCostGroupReq> costGroupList = EntityUtil.copyList(defaultCostGroupList, SeriesTripDefaultCostGroupReq.class);
                        tripDefaultCostReq.setCostGroupList(costGroupList);
                    }
                    //调整费用
                    List<SeriesTripDefaultFeeAdjust> defaultFeeAdjustList = defaultFeeAdjustService.list(new QueryWrapper<SeriesTripDefaultFeeAdjust>().lambda().eq(SeriesTripDefaultFeeAdjust::getCostId, tripDefaultCostReq.getId()));
                    if (CollectionUtils.isNotEmpty(defaultFeeAdjustList)) {
                        List<SeriesTripDefaultFeeAdjustReq> feeAdjustList = EntityUtil.copyList(defaultFeeAdjustList, SeriesTripDefaultFeeAdjustReq.class);
                        tripDefaultCostReq.setFeeAdjustList(feeAdjustList);
                    }
                    //代收费用
                    List<SeriesTripDefaultCostCollection> defaultCostCollectionList = defaultCostCollectionService.list(new QueryWrapper<SeriesTripDefaultCostCollection>().lambda().eq(SeriesTripDefaultCostCollection::getCostId, tripDefaultCostReq.getId()));
                    if (CollectionUtils.isNotEmpty(defaultCostCollectionList)) {
                        List<SeriesTripDefaultCostCollectionReq> costCollectionList = new ArrayList<>();
                        for (SeriesTripDefaultCostCollection seriesTripDefaultCostCollection : defaultCostCollectionList) {
                            SeriesTripDefaultCostCollectionReq seriesTripDefaultCostCollectionReq = new SeriesTripDefaultCostCollectionReq();
                            BeanUtils.copyProperties(seriesTripDefaultCostCollection, seriesTripDefaultCostCollectionReq);
                            List<SeriesTripDefaultCostCollectionTouristType> defaultCostCollectionTouristTypeList = defaultCostCollectionTouristTypeService.list(new QueryWrapper<SeriesTripDefaultCostCollectionTouristType>().lambda().eq(SeriesTripDefaultCostCollectionTouristType::getCollectionId, seriesTripDefaultCostCollection.getId()));
                            List<SeriesTripDefaultCostCollectionTypeReq> costCollectionTypeList = EntityUtil.copyList(defaultCostCollectionTouristTypeList, SeriesTripDefaultCostCollectionTypeReq.class);
                            seriesTripDefaultCostCollectionReq.setCostCollectionTypeList(costCollectionTypeList);
                            costCollectionList.add(seriesTripDefaultCostCollectionReq);
                        }
                        tripDefaultCostReq.setCostCollectionList(costCollectionList);
                    }
                    tripDefaultCostReqList.add(tripDefaultCostReq);
                }
                tripCostSharingReq.setCostReqList(tripDefaultCostReqList);
            }
        }

        return tripCostSharingReq;
    }


    /**
     * 新增默认成本分摊
     *
     * @param tripCostSharingReq 成本分摊属性
     * @param tripId             行程id
     */
    @Transactional(rollbackFor = Exception.class)
    void defaultCost(TripCostSharingReq tripCostSharingReq, Long tripId,Long userId) {

        baseMapper.delete(new QueryWrapper<SeriesTripDefaultCost>().lambda().eq(SeriesTripDefaultCost::getTripId, tripId));
        defaultCostGroupService.remove(new QueryWrapper<SeriesTripDefaultCostGroup>().lambda().eq(SeriesTripDefaultCostGroup::getTripId, tripId));
        defaultCostCollectionService.remove(new QueryWrapper<SeriesTripDefaultCostCollection>().lambda().eq(SeriesTripDefaultCostCollection::getTripId, tripId));
        defaultFeeAdjustService.remove(new QueryWrapper<SeriesTripDefaultFeeAdjust>().lambda().eq(SeriesTripDefaultFeeAdjust::getTripId, tripId));
        defaultCostCollectionTouristTypeService.remove(new QueryWrapper<SeriesTripDefaultCostCollectionTouristType>().lambda().eq(SeriesTripDefaultCostCollectionTouristType::getTripId, tripId));

        //添加成本费用
        List<TripDefaultCostReq> costReqList = tripCostSharingReq.getCostReqList();
        //默认成本表
        List<SeriesTripDefaultCost> seriesTripDefaultCostList = new ArrayList<>();
        //团综合成本
        List<SeriesTripDefaultCostGroup> seriesTripDefaultCostGroupList = new ArrayList<>();
        //代收费用
        List<SeriesTripDefaultCostCollection> seriesTripDefaultCostCollectionList = new ArrayList<>();
        //代收费用旅客类型
        List<SeriesTripDefaultCostCollectionTouristType> seriesTripDefaultCostCollectionTouristTypeList = new ArrayList<>();
        // 调整项目
        List<SeriesTripDefaultFeeAdjust> seriesTripDefaultFeeAdjustList = new ArrayList<>();

        //查询旅客类型主数据列表
        Map<Long, MainDataTouristTypeListDTO> mainDataTouristTypeMap = queryMainDataTouristTypeList();

        //设置默认旅客信息
        List<SeriesTripTouristType> seriesTripTouristTypeList = seriesTripTouristTypeMapper.selectList(new QueryWrapper<SeriesTripTouristType>().lambda().eq(SeriesTripTouristType::getTripId, tripId).orderByDesc(SeriesTripTouristType::getSort));
        if (Assist.isNotEmpty(seriesTripTouristTypeList)) {
            for (SeriesTripTouristType touristType : seriesTripTouristTypeList) {
                MainDataTouristTypeListDTO mainDataTouristType = mainDataTouristTypeMap.get(touristType.getMainDataId());
                if (mainDataTouristType != null) {
                    fillUpdateEntityUserId(touristType, userId);
                    touristType.setAgeBegin(mainDataTouristType.getAgeBegin());
                    touristType.setAgeEnd(mainDataTouristType.getAgeEnd());
                    touristType.setGmtModified(LocalDateTime.now());
                    seriesTripTouristTypeMapper.updateById(touristType);
                }
            }
        }

        SeriesTripDefaultCost seriesTripDefaultCost = null;
        for (TripDefaultCostReq tripDefaultCostReq : costReqList) {
            seriesTripDefaultCost = newCreateEntityUserId(SeriesTripDefaultCost.class, tripDefaultCostReq.getSellingPriceReq(), userId);

            if (seriesTripDefaultCost.getGroupCustomerPrice() == null) {
                throwException(SystemError.TOUR_30011);
            }
            //TODO 添加档案文件
            seriesTripDefaultCost.setQuotationFileUrl(tripDefaultCostReq.getQuotationFileUrl());
            Long costId = seriesTripDefaultCost.getId();
            seriesTripDefaultCost.setTripId(tripId);
            seriesTripDefaultCostList.add(seriesTripDefaultCost);
            //团综合成本
            List<SeriesTripDefaultCostGroupReq> costGroupList = tripDefaultCostReq.getCostGroupList();
            List<SeriesTripDefaultCostGroup> defaultCostGroupList = EntityUtil.copyList(costGroupList, SeriesTripDefaultCostGroup.class);
            defaultCostGroupList.forEach(costGroup -> {
                costGroup.setCostId(costId);
                costGroup.setTripId(tripId);
            });
            seriesTripDefaultCostGroupList.addAll(defaultCostGroupList);

            List<SeriesTripDefaultCostCollectionReq> costCollectionList = tripDefaultCostReq.getCostCollectionList();
            List<SeriesTripDefaultCostCollection> defaultCostCollectionList = new ArrayList<>();

            //代收费用
            for (SeriesTripDefaultCostCollectionReq seriesTripDefaultCostCollectionReq : costCollectionList) {
                SeriesTripDefaultCostCollection seriesTripDefaultCostCollection = newCreateEntityUserId(SeriesTripDefaultCostCollection.class, seriesTripDefaultCostCollectionReq, userId);
                seriesTripDefaultCostCollection.setCostId(costId);
                seriesTripDefaultCostCollection.setTripId(tripId);
                List<SeriesTripDefaultCostCollectionTypeReq> costCollectionTypeList = seriesTripDefaultCostCollectionReq.getCostCollectionTypeList();
                List<SeriesTripDefaultCostCollectionTouristType> touristTypeList = EntityUtil.copyList(costCollectionTypeList, SeriesTripDefaultCostCollectionTouristType.class);
                touristTypeList.forEach(touristType -> {
                    touristType.setCollectionId(seriesTripDefaultCostCollection.getId());
                    touristType.setTripId(tripId);
//                    touristType.setTouristTypeId();
                    touristType.setId(IdWorker.getId());

                    MainDataTouristTypeListDTO mainDataTouristType = mainDataTouristTypeMap.get(touristType.getTouristTypeId());
                    if (mainDataTouristType != null) {
                        touristType.setAgeBegin(mainDataTouristType.getAgeBegin());
                        touristType.setAgeEnd(mainDataTouristType.getAgeEnd());
                    }
                });
                seriesTripDefaultCostCollectionTouristTypeList.addAll(touristTypeList);

                defaultCostCollectionList.add(seriesTripDefaultCostCollection);
            }

            defaultCostCollectionList.forEach(costCollection -> {
                costCollection.setCostId(costId);
                costCollection.setTripId(tripId);
            });
            seriesTripDefaultCostCollectionList.addAll(defaultCostCollectionList);

            //调整项目
            List<SeriesTripDefaultFeeAdjustReq> feeAdjustList = tripDefaultCostReq.getFeeAdjustList();
            List<SeriesTripDefaultFeeAdjust> defaultFeeAdjustList = EntityUtil.copyList(feeAdjustList, SeriesTripDefaultFeeAdjust.class);
            fillCreateEntityUserId(defaultFeeAdjustList, userId);
            defaultFeeAdjustList.forEach(feeAdjust -> {
                feeAdjust.setCostId(costId);
                feeAdjust.setTripId(tripId);
            });
            seriesTripDefaultFeeAdjustList.addAll(defaultFeeAdjustList);
        }
        super.saveBatch(seriesTripDefaultCostList);
        defaultCostGroupService.saveBatch(seriesTripDefaultCostGroupList);
        defaultCostCollectionService.saveBatch(seriesTripDefaultCostCollectionList);
        defaultFeeAdjustService.saveBatch(seriesTripDefaultFeeAdjustList);
        defaultCostCollectionTouristTypeService.saveBatch(seriesTripDefaultCostCollectionTouristTypeList);
        if (tripCostSharingReq.getCommitType()==2) {
            Integer update = createApproval(tripId, userId);
            if (update <= 0){
                throw new BusinessException(SystemError.SYS_408);
            }
        }
    }


    /**
     * 自定义成本分摊
     *
     * @param tripCostSharingReq 成本分摊入参
     * @param tripId             行程id
     */
    @Transactional(rollbackFor = Exception.class)
    void customCost(TripCostSharingReq tripCostSharingReq, Long tripId,Long userId) {
        customCostService.remove(new QueryWrapper<SeriesTripCustomCost>().lambda().eq(SeriesTripCustomCost::getTripId, tripId));
        customCostGroupService.remove(new QueryWrapper<SeriesTripCustomCostGroup>().lambda().eq(SeriesTripCustomCostGroup::getTripId, tripId));
        customCostCollectionService.remove(new QueryWrapper<SeriesTripCustomCostCollection>().lambda().eq(SeriesTripCustomCostCollection::getTripId, tripId));
        customFeeAdjustService.remove(new QueryWrapper<SeriesTripCustomFeeAdjust>().lambda().eq(SeriesTripCustomFeeAdjust::getTripId, tripId));
        customCostCollectionTouristTypeService.remove(new QueryWrapper<SeriesTripCustomCostCollectionTouristType>().lambda().eq(SeriesTripCustomCostCollectionTouristType::getTripId, tripId));
        customCostGroupAgeService.remove(new QueryWrapper<SeriesTripCustomCostGroupAge>().lambda().eq(SeriesTripCustomCostGroupAge::getTripId, tripId));
        customFeeAdjustTouristTypeService.remove(new QueryWrapper<SeriesTripCustomFeeAdjustTouristType>().lambda().eq(SeriesTripCustomFeeAdjustTouristType::getTripId, tripId));
        customDateSubsectionService.remove(new QueryWrapper<SeriesTripCustomDateSubsection>().lambda().eq(SeriesTripCustomDateSubsection::getTripId,tripId));
        //修改自定义年龄段属性值
        TripCustomTreeReq tripCustomTree = tripCostSharingReq.getTripCustomTree();
        List<TripCustomTreePassengerReq> passengerList = tripCustomTree.getPassengerList();
        List<SeriesTripTouristType> seriesTripTouristTypeList = newUpdateEntityListUserId(SeriesTripTouristType.class, passengerList, userId);
        tripTouristTypeService.updateBatchById(seriesTripTouristTypeList);
        HashMap<String, SeriesTripTouristType> tripTouristTypeHashMap = new HashMap<>(seriesTripTouristTypeList.size());
        for (SeriesTripTouristType seriesTripTouristType : seriesTripTouristTypeList) {
            String key = seriesTripTouristType.getMainDataId() + ":" + seriesTripTouristType.getAgeBegin() + seriesTripTouristType.getAgeEnd();
            tripTouristTypeHashMap.put(key, seriesTripTouristType);
        }

        //自定义日期段
        List<TripCustomTreeDateReq> dateList = tripCustomTree.getDateList();
        List<SeriesTripCustomDateSubsection> customDateSubsectionList = EntityUtil.copyList(dateList, SeriesTripCustomDateSubsection.class);
        customDateSubsectionList.forEach(data->{
            data.setTripId(tripId);
            data.setId(SnowflakeIdWorker.nextId());
        });
        customDateSubsectionService.saveOrUpdateBatch(customDateSubsectionList);
        HashMap<String, SeriesTripCustomDateSubsection> customDateSubsectionHashMap = new HashMap<>(customDateSubsectionList.size());
        for (SeriesTripCustomDateSubsection seriesTripCustomDateSubsection : customDateSubsectionList) {
            String key = seriesTripCustomDateSubsection.getSubsectionId() + ":" + seriesTripCustomDateSubsection.getDateType() + seriesTripCustomDateSubsection.getCheckWeekDays() + seriesTripCustomDateSubsection.getDateBegin() + seriesTripCustomDateSubsection.getDateEnd();
            customDateSubsectionHashMap.put(key, seriesTripCustomDateSubsection);
        }


        List<TripCustomCostReq> customCostReqList = tripCostSharingReq.getCustomCostReqList();
        //自定义成本费表
        List<SeriesTripCustomCost> customCostList = new ArrayList<>();
        //销售价格信息
        List<SeriesTripCustomSalePrice> customSalePriceList = new ArrayList<>();
        //团综合成本
        List<SeriesTripCustomCostGroup> customCostGroupList = new ArrayList<>();
        //团综合成本自定义值
        List<SeriesTripCustomCostGroupAge> customCostGroupAgeList = new ArrayList<>();
        //代收费用
        List<SeriesTripCustomCostCollection> customCostCollectionList = new ArrayList<>();
        //代收费用旅客类型
        List<SeriesTripCustomCostCollectionTouristType> customCostCollectionTouristTypeList = new ArrayList<>();
        // 调整项目
        List<SeriesTripCustomFeeAdjust> customFeeAdjustList = new ArrayList<>();
        //调整项目自定义值
        List<SeriesTripCustomFeeAdjustTouristType> customFeeAdjustTouristTypeList = new ArrayList<>();

        SeriesTripCustomCost seriesTripCustomCost = null;
        for (TripCustomCostReq tripCustomCostReq : customCostReqList) {
            //成本主表
            seriesTripCustomCost = newCreateEntityUserId(SeriesTripCustomCost.class, tripCustomCostReq, userId);
            Long costId = seriesTripCustomCost.getId();
            customCostList.add(seriesTripCustomCost);
            //销售价格设置
            List<TripCustomSalePriceReq> salePriceList = tripCustomCostReq.getCustomSalePriceList();
            if (CollectionUtils.isNotEmpty(salePriceList)) {
                SeriesTripCustomSalePrice tripCustomSalePrice = null;
                for (TripCustomSalePriceReq tripCustomSalePriceReq : salePriceList) {
                    tripCustomSalePrice = new SeriesTripCustomSalePrice();
                    //TODO
                    BeanUtils.copyProperties(tripCustomSalePriceReq, tripCustomSalePrice);

                    tripCustomSalePrice.setTripTouristTypeId(tripCustomSalePriceReq.getPassengerTypeId());
//                    if (tripCustomSalePrice.getGroupCustomerPrice() == null) {
//                        throwException(SystemError.TOUR_30011);
//                    }

                    tripCustomSalePrice.setCostId(costId);
                    tripCustomSalePrice.setTripId(tripId);
                    String dateKey = tripCustomSalePriceReq.getSubsectionId() + ":" + tripCustomSalePriceReq.getDateType() + tripCustomSalePriceReq.getCheckWeekDays() + tripCustomSalePriceReq.getDateBegin() + tripCustomSalePriceReq.getDateEnd();
                    SeriesTripCustomDateSubsection customDateSubsection = customDateSubsectionHashMap.get(dateKey);
                    if (customDateSubsection != null) {
                        tripCustomSalePrice.setDateSubsectionId(customDateSubsection.getId());
                    }
                    String typeKey = tripCustomSalePriceReq.getMainDataId() + ":" + tripCustomSalePriceReq.getAgeBegin() + tripCustomSalePriceReq.getAgeEnd();
                    SeriesTripTouristType seriesTripTouristType = tripTouristTypeHashMap.get(typeKey);
                    if (seriesTripTouristType != null) {
                        tripCustomSalePrice.setTripTouristTypeId(seriesTripTouristType.getId());
                    }
                    //TODO
                    customSalePriceList.add(tripCustomSalePrice);
                }
            }
            //代收费用
            List<SeriesTripDefaultCostCollectionReq> costCollectionList = tripCustomCostReq.getCostCollectionList();
            if (CollectionUtils.isNotEmpty(costCollectionList)) {
                for (SeriesTripDefaultCostCollectionReq seriesTripDefaultCostCollectionReq : costCollectionList) {
                    Long costCollectionId = IdWorker.getId();
                    seriesTripDefaultCostCollectionReq.setCostId(costId);
                    seriesTripDefaultCostCollectionReq.setTripId(tripId);
                    seriesTripDefaultCostCollectionReq.setId(costCollectionId);
                    List<SeriesTripDefaultCostCollectionTypeReq> costCollectionTypeList = seriesTripDefaultCostCollectionReq.getCostCollectionTypeList();
                    List<SeriesTripCustomCostCollectionTouristType> touristTypeList = EntityUtil.copyList(costCollectionTypeList, SeriesTripCustomCostCollectionTouristType.class);
                    touristTypeList.forEach(touristType -> {
                        touristType.setCollectionId(costCollectionId);
                        touristType.setTripId(tripId);
                        touristType.setId(IdWorker.getId());
                    });
                    customCostCollectionTouristTypeList.addAll(touristTypeList);
                }
                List<SeriesTripCustomCostCollection> collectionList = EntityUtil.copyList(costCollectionList, SeriesTripCustomCostCollection.class);
                //覆蓋了id 導致子表不能關聯
                //fillCreateEntityUserId(collectionList, userId);
                collectionList.forEach(costCollection -> {
                    costCollection.setCostId(costId);
                    costCollection.setTripId(tripId);
                });
                customCostCollectionList.addAll(collectionList);
            }
            //团综合成本
            List<SeriesTripCustomCostGroupReq> costGroupList = tripCustomCostReq.getCostGroupList();

            SeriesTripCustomCostGroup seriesTripCustomCostGroup = null;
            for (SeriesTripCustomCostGroupReq seriesTripCustomCostGroupReq : costGroupList) {
                seriesTripCustomCostGroup = new SeriesTripCustomCostGroup();
                Long costGroupId = IdWorker.getId();
                BeanUtils.copyProperties(seriesTripCustomCostGroupReq, seriesTripCustomCostGroup);
                seriesTripCustomCostGroup.setId(costGroupId);
                seriesTripCustomCostGroup.setCostId(costId);
                seriesTripCustomCostGroup.setTripId(tripId);
                customCostGroupList.add(seriesTripCustomCostGroup);
                //团综合成本自定义值表
                List<CustomCostReq> customValueList = seriesTripCustomCostGroupReq.getCustomCostList();
                if (CollectionUtils.isNotEmpty(customValueList)) {
                    SeriesTripCustomCostGroupAge seriesTripCustomCostGroupAge = null;
                    for (CustomCostReq customCostReq : customValueList) {
                        seriesTripCustomCostGroupAge = new SeriesTripCustomCostGroupAge();
                        seriesTripCustomCostGroupAge.setGroupCostId(costGroupId);
                        seriesTripCustomCostGroupAge.setCostId(costId);
                        seriesTripCustomCostGroupAge.setPrice(customCostReq.getPrice());
                        String dateKey = customCostReq.getSubsectionId() + ":" + customCostReq.getDateType() + customCostReq.getCheckWeekDays() + customCostReq.getDateBegin() + customCostReq.getDateEnd();
                        SeriesTripCustomDateSubsection customDateSubsection = customDateSubsectionHashMap.get(dateKey);
                        if (customDateSubsection != null) {
                            seriesTripCustomCostGroupAge.setDateSubsectionId(customDateSubsection.getId());
                        }
                        String typeKey = customCostReq.getMainDataId() + ":" + customCostReq.getAgeBegin() + customCostReq.getAgeEnd();
                        SeriesTripTouristType seriesTripTouristType = tripTouristTypeHashMap.get(typeKey);
                        if (customDateSubsection != null) {
                            seriesTripCustomCostGroupAge.setTripTouristTypeId(seriesTripTouristType.getId());
                        }
                        customCostGroupAgeList.add(seriesTripCustomCostGroupAge);
                    }
                }

            }

            //自定义调整项目
            List<SeriesTripCustomFeeAdjustReq> feeAdjustList = tripCustomCostReq.getFeeAdjustList();

            SeriesTripCustomFeeAdjust seriesTripCustomFeeAdjust = null;
            for (SeriesTripCustomFeeAdjustReq seriesTripCustomFeeAdjustReq : feeAdjustList) {
                seriesTripCustomFeeAdjust = newCreateEntityUserId(SeriesTripCustomFeeAdjust.class, seriesTripCustomFeeAdjustReq, userId);
                Long feeAdjustId = seriesTripCustomFeeAdjust.getId();
                seriesTripCustomFeeAdjust.setCostId(costId);
                seriesTripCustomFeeAdjust.setTripId(tripId);
                customFeeAdjustList.add(seriesTripCustomFeeAdjust);
                //自定义成本值表
                List<CustomCostReq> customValueList = seriesTripCustomFeeAdjustReq.getCustomCostList();
                if (CollectionUtils.isNotEmpty(customValueList)) {
                    SeriesTripCustomFeeAdjustTouristType seriesTripCustomFeeAdjustTouristType = null;
                    for (CustomCostReq customCostReq : customValueList) {
                        seriesTripCustomFeeAdjustTouristType = new SeriesTripCustomFeeAdjustTouristType();
                        seriesTripCustomFeeAdjustTouristType.setAdjustId(feeAdjustId);
                        seriesTripCustomFeeAdjustTouristType.setCostId(costId);
                        seriesTripCustomFeeAdjustTouristType.setPrice(customCostReq.getPrice());
                        String dateKey = customCostReq.getSubsectionId() + ":" + customCostReq.getDateType() + customCostReq.getCheckWeekDays() + customCostReq.getDateBegin() + customCostReq.getDateEnd();
                        SeriesTripCustomDateSubsection customDateSubsection = customDateSubsectionHashMap.get(dateKey);
                        if (customDateSubsection != null) {
                            seriesTripCustomFeeAdjustTouristType.setDateSubsectionId(customDateSubsection.getId());
                        }
                        String typeKey = customCostReq.getMainDataId() + ":" + customCostReq.getAgeBegin() + customCostReq.getAgeEnd();
                        SeriesTripTouristType seriesTripTouristType = tripTouristTypeHashMap.get(typeKey);
                        if (customDateSubsection != null) {
                            seriesTripCustomFeeAdjustTouristType.setTripTouristTypeId(seriesTripTouristType.getId());
                        }
                        customFeeAdjustTouristTypeList.add(seriesTripCustomFeeAdjustTouristType);
                    }
                }
            }
        }
        customCostService.saveBatch(customCostList);
        customSalePriceService.saveBatch(customSalePriceList);
        customCostCollectionService.saveBatch(customCostCollectionList);
        customCostCollectionTouristTypeService.saveBatch(customCostCollectionTouristTypeList);
        customCostGroupService.saveBatch(customCostGroupList);
        customCostGroupAgeService.saveBatch(customCostGroupAgeList);
        customFeeAdjustService.saveBatch(customFeeAdjustList);
        customFeeAdjustTouristTypeService.saveBatch(customFeeAdjustTouristTypeList);
        if (tripCostSharingReq.getCommitType()==2) {
            Integer update = createApproval(tripId, userId);
            if (update <= 0){
                throw new BusinessException(SystemError.SYS_408);
            }
        }
    }

    private Integer createApproval(Long tripId,Long userId){

        SeriesTrip seriesTrip =new SeriesTrip();
        seriesTrip.setId(tripId);
        long approvalTypeId = SysApprovalType.TRAVEl_OUTTRA.getCode();
        Result<ApprovalStartDTO> result = sysApprovalService.start(SystemBusinessType.OUTBOUND_TRAVEL.getCode(), approvalTypeId, seriesTrip.getId(), userId, null, null);
        if (!result.isSuccess()) {
            throw new BusinessException(SystemError.SYS_408);
        }
        seriesTrip.setApprovalId(result.getData().getApprovalId());
        seriesTrip.setStatus(TripStatusConstants.UNDER_APPROVAL.getCode());
        return seriesTripMapper.updateById(seriesTrip);
    }

    /**
     * 查询旅客类型主数据列表
     * @return
     */
    private Map<Long, MainDataTouristTypeListDTO> queryMainDataTouristTypeList() {
    	MainDataTouristTypeListReq callReq = new MainDataTouristTypeListReq();
    	callReq.setStatus(0);
    	List<MainDataTouristTypeListDTO> list = mainDataTouristTypeService.list(callReq).getRecords();
    	return Assist.forEachToMap(list, MainDataTouristTypeListDTO::getId);
    }
}
